package cn.kgc.tangcco.controller;

import cn.binarywang.tools.generator.ChineseIDCardNumberGenerator;
import cn.binarywang.tools.generator.ChineseNameGenerator;
import cn.binarywang.tools.generator.base.GenericGenerator;
import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.model.ResultCode;
import cn.kgc.tangcco.pojo.Emp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/25 14:59
 */
@Controller
@RequestMapping(value = "/freemaker")
public class FreemarkerHandler {
    private static List<Emp> emps = new ArrayList<>();

    static {
        GenericGenerator chineseNameGenerator = ChineseNameGenerator.getInstance();
        GenericGenerator chineseIDCardNumberGenerator = ChineseIDCardNumberGenerator.getInstance();

        for (int i = 0; i < 5; i++) {
            emps.add(new Emp(i + 1, chineseNameGenerator.generate(), chineseIDCardNumberGenerator.generate()));
        }
    }

    @GetMapping(value = "/forwordFtl")
    public String forwordFtl(Model model) {
        model.addAttribute("slogan", "桃李不言下自成蹊");
        return "freemarker";
    }

    @GetMapping(value = "/forwordTable")
    public String forwordTable(Model model) {
        model.addAttribute("emps", emps);
        return "table";
    }

    @PutMapping(value = "/add")
    @ResponseBody
    public ResponseText add(Emp emp) {
        if (StringUtils.isBlank(emp.getName()) || StringUtils.isBlank(emp.getIdCard())) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        emp.setId(emps.get(emps.size() - 1).getId() + 1);
        if (emps.add(emp)) {
            return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
        }
        return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }

    @DeleteMapping(value = "/del")
    @ResponseBody
    public ResponseText del(@RequestBody List<Integer> ids) {
        if (ids.size() == 0) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        int oldSize = emps.size();
        for (int i = 0; i < ids.size(); i++) {
            int finalI = i;
            emps.removeIf(emp -> emp.getId() == ids.get(finalI));
        }
        if (oldSize > emps.size()){
            return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
        }
        return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }

    @GetMapping(value = "/info/{id}")
    @ResponseBody
    public ResponseText info(@PathVariable(value = "id") Integer id) {
        List<Emp> empList = emps.stream().filter(emp -> emp.getId() == id).collect(Collectors.toList());
        if (empList != null && empList.size() > 0) {
            return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), empList.get(0));
        }
        return new ResponseText(ResultCode.DATA_EMPTY.getCode(), ResultCode.DATA_EMPTY.getMsg(), null);
    }

    @PutMapping(value = "/save")
    @ResponseBody
    public ResponseText save(Emp emp){
        Iterator<Emp> it = emps.listIterator();
        for (int i = 0; i < emps.size(); i++) {
            Emp oldEmp = emps.get(i);
            if (oldEmp.getId() == emp.getId()) {
                emps.set(i, emp);
                return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
            }
        }

        return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }
}
